package com.example.lll.lll_20_weather;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class ForecastAdapter extends RecyclerView.Adapter<ForecastAdapter.ViewHolder> {
    private List<WeatherResponse.Forecast> forecastList;

    public ForecastAdapter(List<WeatherResponse.Forecast> forecastList) {
        this.forecastList = forecastList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_forecast, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        WeatherResponse.Forecast forecast = forecastList.get(position);
        holder.tvDate.setText(forecast.getYmd());
        holder.tvWeatherType.setText(forecast.getType());
        holder.tvTemperatureRange.setText(forecast.getLow() + " ~ " + forecast.getHigh());

        setWeatherIcon(holder.ivWeatherIcon,forecast.getType());
        setItemBackground(holder.itemView,forecast.getType());
    }

    private void setWeatherIcon(ImageView imageView,String weatherType){
        int iconResource;
        if (weatherType.contains("晴")) {
            iconResource = R.drawable.sunny;
        } else if (weatherType.contains("云") || weatherType.contains("阴")) {
            iconResource = R.drawable.cloudy;
        } else if (weatherType.contains("雨")) {
            iconResource = R.drawable.rainy;
        } else if (weatherType.contains("雪")) {
            iconResource = R.drawable.snowy;
        } else {
            iconResource = R.drawable.default_weather;
        }
        imageView.setImageResource(iconResource);
    }

    private void setItemBackground(View view,String weatherType){
        int bgColor;
        if (weatherType.contains("晴")){
            bgColor = Color.argb(150, 255, 204, 0);
        }else if (weatherType.contains("云") || weatherType.contains("阴")) {
            bgColor = Color.argb(150, 189, 189, 189); // 半透明灰色
        } else if (weatherType.contains("雨")) {
            bgColor = Color.argb(150, 66, 165, 245); // 半透明蓝色
        } else if (weatherType.contains("雪")) {
            bgColor = Color.argb(150, 255, 255, 255); // 半透明白色
        } else {
            bgColor = Color.argb(150, 176, 190, 197); // 默认半透明灰蓝色
        }

        GradientDrawable bg = new GradientDrawable();
        bg.setColor(bgColor);
        bg.setCornerRadius(16f);
        view.setBackground(bg);
    }
    @Override
    public int getItemCount() {
        return forecastList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate, tvWeatherType, tvTemperatureRange;
        ImageView ivWeatherIcon;

        ViewHolder(View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvWeatherType = itemView.findViewById(R.id.tvWeatherType);
            tvTemperatureRange = itemView.findViewById(R.id.tvTemperatureRange);
            ivWeatherIcon = itemView.findViewById(R.id.ivWeatherIcon);
        }
    }
}
